package com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment;

import com.freak.httphelper.ApiCallback;
import com.freak.httphelper.HttpMethods;
import com.freak.httphelper.RxPresenter;
import com.freak.httphelper.SubscriberCallBack;
import com.freak.neteasecloudmusic.app.api.ApiService;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.HotSongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.RecommendEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.google.gson.Gson;

import javax.inject.Inject;

import io.reactivex.Observable;

public class SongListFragmentPresenter extends RxPresenter<SongListFragmentContract.View> implements SongListFragmentContract.Presenter {
    private ApiService mApiService = HttpMethods.getInstance().create(ApiService.class);

    public SongListFragmentPresenter() {

    }

    @Inject
    public SongListFragmentPresenter(SongListFragmentContract.View view) {
        mView = view;
    }

    /**
     * 获取歌曲分类列表
     *
     * @param order 可选值为 'new' 和 'hot', 分别对应最新和最热 , 默认为 'hot'
     * @param cat   比如 " 华语 "、" 古风 " 、" 欧美 "、" 流行 ", 默认为 "全部",可从歌单分类接口获取(/playlist/catlist)
     * @param limit 取出歌单数量 , 默认为 20
     */
    @Override
    public void loadSongListCategoryList(String order, String cat, final int limit, final int offset) {
        Observable<SongListEntity> observable = mApiService.loadSongListCategoryList(order, cat, limit, offset);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<SongListEntity>() {
            @Override
            public void onSuccess(SongListEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {

                    mView.loadSongListCategoryListSuccess(model);
                } else {
                    mView.showToast(model.getMsg());
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.e(msg);
                mView.showToast(msg);
            }
        }));
    }

    /**
     * 精品歌单
     *
     * @param cat    比如 " 华语 "、" 古风 " 、" 欧美 "、" 流行 ", 默认为 "全部",可从歌单分类接口获取(/playlist/catlist)
     * @param limit  取出歌单数量 , 默认为 20
     * @param offset
     */
    @Override
    public void loadHotSongListCategoryList(String cat, final int limit, final int offset) {
        Observable<HotSongListEntity> observable = mApiService.loadQualitySongList(cat, limit, offset);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<HotSongListEntity>() {
            @Override
            public void onSuccess(HotSongListEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {

                    mView.loadHotSongListCategoryListSuccess(model);
                } else {
                    mView.showToast(model.getMsg());
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getRecommendSongList(int limit) {
        Observable<RecommendEntity> observable = mApiService.getRecommendSongList(limit);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<RecommendEntity>() {
            @Override
            public void onSuccess(RecommendEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    mView.getRecommendSongListSuccess(model);
                } else {
                    mView.showToast("获取失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getSearch(String uid) {
        Observable<Object> observable = mApiService.getSearch(uid);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));

            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

}
